package natappvip.liujinliang.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName:  PropertiesUtil   
 * @Despt: 从配置文件获取配置参数   
 * @Auth: Liujinliang 
 * @Date: 2018年7月29日 下午9:26:51
 */
public class PropertiesUtil {
	
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

	private static Properties props;
	
	static {
		String fileName = "common.properties";
		props = new Properties();
		try {
			props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName)));
  		} catch (IOException e) {
  			logger.info("配置文件读取异常，错误信息:{}", e);
  		}
	}
	
	/**
	 * @Title: getProperty   
	 * @Despt: 根据参数名称获取参数值 
	 * @param: @param key
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String getProperty(String key) {
		String value = props.getProperty(key.trim());
		if(value.isEmpty()) {
			return null;
		}
		return value.trim();
	}
	
	/**
	 * @Title: getProperty   
	 * @Despt: 根据参数名称获取带有默认值的参数值，   
	 * @param: @param key
	 * @param: @param defaultValue
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String getProperty(String key, String defaultValue) {
		String value = props.getProperty(key.trim());
		if(value.isEmpty()) {
			return defaultValue;
		}
		return value.trim();
	}
}
